#ifndef IMAGE_H
#define IMAGE_H

#include "ByteStream.h"
#include "ImageSegment.h"

class Image
{
public:
	Image();
	Image(unsigned int newWidth, unsigned int newHeight);
	~Image();

	void SetComponent(unsigned int x, unsigned int y, unsigned char intensity);
	unsigned char GetComponent(unsigned int x, unsigned int y) const;

	unsigned char& operator()(unsigned int x, unsigned int y);

	virtual void ReadFromFile(const char* filename);
	virtual void WriteToFile(const char* filename);

	void ReadRawFromSegment(unsigned int x0, unsigned int y0, const ImageSegment& seg);
	void ReadRawFromData(const void *imagedata, unsigned int size);
	void ReadRawFromFile(const char* filename);
	void WriteRawToFile(const char* filename);
	void ReadRawFromStream(ByteStream& stream);

	void SetDimension(unsigned int newWidth, unsigned int newHeight);

	ImageSegment* SegmentFromOffset(unsigned int x0, unsigned int y0, unsigned int blockWidth, unsigned int blockHeight);
	ImageSegment* NextSegment();
	void InitializeSegment(unsigned int blockWidth, unsigned int blockHeight);

	Image& operator=(const Image& rhs);


	unsigned int Size() const
	{
		return width * height;
	}

	unsigned int Width() const 
	{ 
		return width; 
	}

	unsigned int Height() const
	{ 
		return height; 
	}

	const unsigned char* ConstData() const
	{
		return data;
	}

	unsigned char* Data()
	{
		return data;
	}



protected:

private:
	Image(const Image& rhs); // copy constructor not implemented yet

	void ClearImage();
	void Clear();

	unsigned char *data;
	unsigned int width, height;

	ImageSegment segment;
};

#endif //IMAGE_H